package com.jiang.server;

import com.jiang.Domain.Message;
import com.jiang.Domain.MessageType;
import com.jiang.Domain.User;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Jiang
 * @version 1.0
 * @date 2023/11/29 13:16
 * 在 9999 端口监听，等待客户连接
 */
public class QQServer {
    private ServerSocket serverSocket = null;
    // 存放用户的集合
//    private static HashMap<String, User> validUsers = new HashMap<>();
    // HashMap 在多线程下是不安全的
    // ConcurrentHashMap 在多线程下是安全的
    private static ConcurrentHashMap<String, User> validUsers = new ConcurrentHashMap<>();


    static {
        validUsers.put("100", new User("100", "123456"));
        validUsers.put("200", new User("200", "123456"));
        validUsers.put("300", new User("300", "123456"));
        validUsers.put("400", new User("400", "123456"));
    }

    // 验证用户是否合法
    private boolean checkUser(String userId, String password) {
        User user = validUsers.get(userId);
        if(user == null) {
            return false;
        }

        if (!user.getPasswd().equals(password)) {
            return false;
        }

        return true;
    }

    public QQServer() {

        try {
            System.out.println("服务器在 9999 端口监听");
            //启动推送新闻的线程
//            new Thread(new SendNewsToAllService()).start();


            serverSocket = new ServerSocket(9999);
            while (true) {
                Socket socket = serverSocket.accept();
                // 读取客户发送的User对象
                ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
                User user = (User) objectInputStream.readObject();

                Message message = new Message();
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());

                // 验证
                if (checkUser(user.getId(), user.getPasswd())) {
                    message.setMesType(MessageType.MESSAGE_LOGIN_SUCCEED);
                    objectOutputStream.writeObject(message);

                    //创建一个和客户端保持通信的线程
                    ServerConnectClientThread serverConnectClientThread = new ServerConnectClientThread(socket, user.getId());
                    new Thread(serverConnectClientThread).start();
                    ManageServerConnectClientThread.addServerConnectClientThread(user.getId(), serverConnectClientThread);

                } else {
                    System.out.println("用户" + user.getId() + "验证失败");
                    message.setMesType(MessageType.MESSAGE_LOGIN_FAIL);
                    objectOutputStream.writeObject(message);
                    socket.close();
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
